package com.letv.ads.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.text.TextUtils;

import com.letv.adlib.sdk.types.AdElement;
import com.letv.adlib.sdk.types.AdElementMime;
import com.letv.ads.view.LetvWebViewActivity;

public final class AdsUtils {

	public static long lastClickTime = 0;
    public static long currentClickTime = 0;

    private AdsUtils() {
    }

    public static boolean checkClickEvent() {
        return checkClickEvent(1000);
    }
	
	public static boolean checkClickEvent(long interval) {
		currentClickTime = System.currentTimeMillis();
		if (currentClickTime - lastClickTime > interval) {
			lastClickTime = currentClickTime;
			return true;
		} else {
			lastClickTime = currentClickTime;
			return false;
		}
	}

    /**
     * 请使用同名重载方法
     *
     * @param context
     * @param url
     * @param skipType
     */
    @Deprecated
    public static void gotoWeb(Context context, String url, int skipType) {
//        skipType = AdElement.AdClickShowType.SHOW_InternalWebView.value();
		if(skipType == AdElement.AdClickShowType.SHOW_ExternalWebBrowser.value()){
			Intent intent = new Intent(Intent.ACTION_VIEW);
			intent.setData(Uri.parse(url));
			intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_FROM_BACKGROUND);
			context.startActivity(intent);
		} else if (skipType == AdElement.AdClickShowType.SHOW_InternalWebView.value()){
//            url = "http://minisite.letv.com/thirdparty/mtestone/index.shtml";
//			LetvWebViewActivity.launch(context, url, "广告");
		}
//		QQWebViewActivity.launch(context, url, "广告");
    }

    public static void gotoWeb(Context context, AdElementMime adElem) {
        int skipType = adElem.clickShowType;
        String url = adElem.getClickThrough();

        if (skipType == AdElement.AdClickShowType.SHOW_ExternalWebBrowser.value()) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(url));
            if (context instanceof Activity) {
                intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
                        | Intent.FLAG_ACTIVITY_NO_HISTORY
                        | Intent.FLAG_FROM_BACKGROUND);
            } else {
                intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
                        | Intent.FLAG_ACTIVITY_NO_HISTORY
                        | Intent.FLAG_FROM_BACKGROUND
                        | Intent.FLAG_ACTIVITY_NEW_TASK);
            }
            context.startActivity(intent);
        } else if (skipType == AdElement.AdClickShowType.SHOW_InternalWebView.value()) {
//            url = "http://minisite.letv.com/thirdparty/mtestone/index.shtml";
            LetvWebViewActivity.launch(context, url, "广告", adElem);
        }
//		QQWebViewActivity.launch(context, url, "广告");
    }

    public static String createExt(String cid, String pid, String vid, String adType, String tfid) {
        StringBuilder builder = new StringBuilder();

        builder.append(getEleString(cid));
		builder.append(";");
		builder.append(getEleString(pid));
		builder.append("_");
		builder.append(getEleString(vid));
		builder.append(";");
		builder.append(getEleString(adType));
		builder.append(";");
		builder.append(getEleString(tfid));
		
		return builder.toString();
	}
	
	public static String createExt(String adType , String tfid){
		StringBuilder builder = new StringBuilder() ;
		
		builder.append(getEleString(adType));
		builder.append(";");
		builder.append(getEleString(tfid));
		
		return builder.toString();
	}
	
	public static String getEleString(String ele){
		if(TextUtils.isEmpty(ele)){
			return "-";
		}
		
		return ele ;
	}
	public static String createAdDuration(String firstAdTime, String secAdTime, String thirdAdTime) {
		StringBuilder builder = new StringBuilder();
		builder.append(getEleString(thirdAdTime));
		builder.append("_");
		builder.append(getEleString(secAdTime));
		builder.append("_");
		builder.append(getEleString(firstAdTime));
		return builder.toString();
	}
	
	public static String getAdSystem(int adSystem){
		if(adSystem==1){
			return "haoye";
		}else {
			return "letv";//暂时数据
		}
	}
    public static boolean isNetworkAvailable(Context context) {
        if (context != null) {
            ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            if (connectivity == null) {
                return false;
            } else {
                NetworkInfo[] info = connectivity.getAllNetworkInfo();
                if (info != null) {
                    for (int i = 0; i < info.length; i++) {
                        if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }
}
